package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.annotation.RequriedPermission;
import cn.wolfcode.wms.domain.Department;
import cn.wolfcode.wms.util.AjaxResult;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @RequriedPermission("部门列表")
    @RequestMapping("query")
    public String query(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo<Department> result = departmentService.query(qo);
        model.addAttribute("result",result);
        return "department/list";
    }
    @RequriedPermission("部门编辑")
    @RequestMapping("input")
    public String input(Model model,Long id){
        if (id != null){
            Department department = departmentService.get(id);
            model.addAttribute("dept",department);
        }
        return "department/input";
    }
    @RequriedPermission("部门保存/更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Department department){
        if (department.getId() == null){
            departmentService.save(department);
        }else {
            departmentService.update(department);
        }
        return new AjaxResult();
    }
    @RequriedPermission("部门删除")
    @RequestMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        if (id != null){
            departmentService.delete(id);
        }
        return new AjaxResult();
    }
}
